import { observable, action, toJS } from 'mobx';
import { Toast, Tools } from 'weaver-mobile';
import { ListViewPage, ListPage, ShareAuth } from 'weaver-mobile-page';
import * as Api from '../../api/UserCenterApi';

const getLabel = Tools.getLabel;

export default class SettingStore {
  @observable isReceive = 0; // 是否接受关注申请
  @observable allowRequest = 1;
  @observable sharePage = 1;
  @observable defaultList = new ListViewPage.Store({ route: '/userCenter/setting/shareSet', id: 'shareSet' });
  @observable defaultNewDatas = {};
  @observable myShareList = new ListPage.Store({ route: '/userCenter/setting/shareSet', id:'myshare' }); // 我的分享

  @action setState = (params = {}) => {
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    });
  }

  /************************************* 设置主页信息 ******************************************/
  @action('基本设置信息获取')
  getBaseSetting = (params = {}) => {
    Api.getBaseSetting(params).then(data => {
      this.isReceive = data.userBlogBaseSetting.isReceive;
      this.allowRequest = data.allowRequest;
    })
  }

  @action('基本设置信息保存')
  saveBaseSetting = (v) =>{
    this.setReceive(v);
    let params = {
      isReceive: this.isReceive
    }
    Api.saveBaseSetting(params).then(result => {
      if (result.api_status === 'true') {
        Toast.success(getLabel(22619 ,'保存成功'), 1);
      } else {
        Toast.fail(getLabel(22620 ,'保存失败'), 1);
      }
    })
  }
  
  @action('更改flag的值')
  setReceive = (value) => {
    this.isReceive = value ? 1 : 0;
  }

  @action('设置主页卸载')
  settingClear = () => {
    this.isReceive = 0;
    this.allowRequest = 1;
  }

  /* ************************************** 分享设置 ***************************************** */
  @action('默认分享')
  getDefaultShareList = () => {
    Api.getDefaultShareList().then(res => {
      this.defaultList.getData({dataKey: res.datas});
      toJS(this.defaultList.data).map((item, index) => {
        if(this.defaultNewDatas[item.typespan]) {
          return this.defaultNewDatas[item.typespan] = item;
        }
      })
      this.defaultList.setState({data: toJS(this.defaultNewDatas)})
    })
  };

  /* **************我的分享********** */
  @action
  myShareInit = () => {
    this.getBlogShareSetListCondition();
  }

  @action('我的分享')
  getBlogShareSetListCondition = (params = {}) => {
    Api.getBlogShareSetListCondition(params).then(res => {
      const {status, datas} = res;
      if (status === '1') {
        // this.myShareList.getData({dataKey: res.datas});
      }
    })
  };
}